from Python_Structure_Algorithm.Python_hash_15_copy import HashTable


class DictADT(HashTable):
    def __setitem__(self, key, value):
        self.add(key, value)

    def __getitem__(self, key):
        if key not in self:
            raise KeyError
        else:
            return self.get(key)

    def items(self):
        for slot in self._table:
            if slot not in (HashTable.UNUSED, HashTable.EMPTY):
                yield (slot.key, slot.value)

    def keys(self):
        for key in self:
            yield key

    def values(self):
        for slot in self._table:
            if slot not in (HashTable.UNUSED, HashTable.EMPTY):
                yield slot.value


def test_dict_adt():
    d = DictADT()
    d['a'] = 1
    assert d['a'] == 1
    d.remove('a')

    l = list(range(30))
    import random
    random.shuffle(l)
    for i in l:
        d[i] = i

    for i in range(30):
        assert d[i] == i

    assert sorted(list(d.keys())) == sorted(l)
    